package Eyjafjalla.effect;

import Eyjafjalla.helper.StringHelper;
import Eyjafjalla.modcore.EyjafjallaTag;
import Eyjafjalla.modifier.FireMarkMod;
import Eyjafjalla.patch.CloudPatch;
import basemod.helpers.CardModifierManager;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Interpolation;
import com.megacrit.cardcrawl.cards.AbstractCard;
import com.megacrit.cardcrawl.cards.CardGroup;
import com.megacrit.cardcrawl.core.CardCrawlGame;
import com.megacrit.cardcrawl.core.Settings;
import com.megacrit.cardcrawl.dungeons.AbstractDungeon;
import com.megacrit.cardcrawl.helpers.ImageMaster;
import com.megacrit.cardcrawl.localization.UIStrings;
import com.megacrit.cardcrawl.rooms.AbstractRoom;
import com.megacrit.cardcrawl.rooms.CampfireUI;
import com.megacrit.cardcrawl.rooms.RestRoom;
import com.megacrit.cardcrawl.vfx.AbstractGameEffect;
import com.megacrit.cardcrawl.vfx.cardManip.ShowCardBrieflyEffect;

import java.util.ArrayList;
import java.util.Iterator;


public class CampfireMarkEffect extends AbstractGameEffect {
    private static final UIStrings uiStrings;
    private static final float DUR = 1.5F;
    private boolean openedScreen = false;
    private Color screenColor;

    public static boolean isFireMarking = false;

    public CampfireMarkEffect(){
        screenColor = AbstractDungeon.fadeColor.cpy();
        duration = DUR;
        screenColor.a = 0.0F;
        AbstractDungeon.overlayMenu.proceedButton.hide();
    }

    private void updateBlackScreenColor() {
        if (this.duration > 1.0F) {
            this.screenColor.a = Interpolation.fade.apply(1.0F, 0.0F, (this.duration - 1.0F) * 2.0F);
        } else {
            this.screenColor.a = Interpolation.fade.apply(0.0F, 1.0F, this.duration / 1.5F);
        }

    }

    @Override
    public void update() {
        if(!AbstractDungeon.isScreenUp){
            duration -= Gdx.graphics.getDeltaTime();
            this.updateBlackScreenColor();
        }

        if(!AbstractDungeon.isScreenUp && !AbstractDungeon.gridSelectScreen.selectedCards.isEmpty() && AbstractDungeon.gridSelectScreen.forUpgrade){
            Iterator<AbstractCard> var1 = AbstractDungeon.gridSelectScreen.selectedCards.iterator();
            while (var1.hasNext()){
                AbstractCard c = var1.next();
                CardModifierManager.addModifier(c,new FireMarkMod());
                AbstractCard tmp = c.makeStatEquivalentCopy();
                AbstractDungeon.effectsQueue.add(new ShowCardBrieflyEffect(tmp));
                AbstractDungeon.topLevelEffects.add(new MarkFireBreathEffect(tmp));
            }
            if(AbstractDungeon.gridSelectScreen.selectedCards.size()>0)
                CardCrawlGame.sound.playA("ATTACK_FIRE", 0.3F);
            AbstractDungeon.gridSelectScreen.selectedCards.clear();
            isFireMarking = false;
            ((RestRoom)AbstractDungeon.getCurrRoom()).fadeIn();
        }

        if(this.duration<1F && !openedScreen){
            openedScreen = true;
            CardGroup tmp = new CardGroup(CardGroup.CardGroupType.UNSPECIFIED);
            tmp.group = getFireCards();
            isFireMarking = true;
            AbstractDungeon.gridSelectScreen.open(tmp,1,uiStrings.TEXT[0],true,false,true,false);
        }

        if(this.duration<0F){
            isFireMarking = false;
            this.isDone = true;
            if(CampfireUI.hidden){
                AbstractRoom.waitTimer = 0F;
                AbstractDungeon.getCurrRoom().phase = AbstractRoom.RoomPhase.COMPLETE;
                ((RestRoom)AbstractDungeon.getCurrRoom()).cutFireSound();
            }
        }
    }

    public static ArrayList<AbstractCard> getFireCards(){
        ArrayList<AbstractCard> firableCards = new ArrayList<>();
        Iterator var2 = AbstractDungeon.player.masterDeck.group.iterator();

        while (var2.hasNext()) {
            AbstractCard c = (AbstractCard) var2.next();
            if ((c.type == AbstractCard.CardType.ATTACK || c.type == AbstractCard.CardType.SKILL || c.type == AbstractCard.CardType.POWER) && (!CloudPatch.CloudField.isFire.get(c)||c.hasTag(EyjafjallaTag.UNLIMITED_FIRE_EYJAFJALLA)) && CloudPatch.CloudField.showFire.get(c)) {
                firableCards.add(c);
            }
        }
        return firableCards;
    }

    @Override
    public void render(SpriteBatch sb) {
        sb.setColor(this.screenColor);
        sb.draw(ImageMaster.WHITE_SQUARE_IMG, 0.0F, 0.0F, (float) Settings.WIDTH, (float)Settings.HEIGHT);
        if (AbstractDungeon.screen == AbstractDungeon.CurrentScreen.GRID) {
            AbstractDungeon.gridSelectScreen.render(sb);
        }
    }

    @Override
    public void dispose() {
        isFireMarking = false;
    }

    static {
        uiStrings = CardCrawlGame.languagePack.getUIString(StringHelper.CAMPFIRE_OPERATION);
    }
}
